<?php
/**
 * Created By PhpStorm
 * Author: patrick wu
 * Date: 2020/10/21
 * Time: 15:14
 */
namespace App\Service\Api;

use App\Http\Model\Mysql\UserModel;
use App\Http\Model\Mysql\OrderIncomeModel;
use App\Http\Model\Mysql\ApplyWithdraw;
use Carbon\Carbon;

class CommissionService extends BaseService
{
    /**
     * 佣金记录列表
     * @return array
     */
    public function commissionTotal()
    {
        $user_info  =   request()->post('api_user');
        $reward_list=   OrderIncomeModel::where(['uid' => $user_info['user_id']])->select(['inid', 'uid', 'oid', 'reward', 'status', 'desc', 'income', 'day_time'])->get()->each(function (&$item) {

                        });
        if ($reward_list->isEmpty()) {
            return [true, []];
        }

        return [true, $reward_list->toArray()];
    }

    /**
     * 佣金记录详情
     * @param int $detail_id
     * @return array
     */
    public function commissionDetail(int $detail_id)
    {
        $user_info      =   request()->post('api_user');
        $detail_info    =   OrderIncomeModel::with([
                                'order' => function ($query) {
                                    $query->select(['id', 'order_no', 'pay_price', 'coupon_price', 'total_price']);
                                }
                            ])->where(['inid' => $detail_id, 'uid' => $user_info['user_id']])->select(['inid', 'uid', 'oid', 'reward', 'status', 'desc', 'income', 'day_time', 'type'])->first();
        if (empty($detail_info)) {
            return [false, '记录不存在'];
        }

        return [true, $detail_info->toArray()];
    }

    /**
     * 用户申请提现
     * @param float $withdraw_money  申请提现金额
     * @return array
     */
    public function commissionWithdraw(float $withdraw_money)
    {
        $user_info  =   request()->post('api_user');

        /**
         * 查看余额是否足够
         */
        if ($withdraw_money > $user_info['balance']) {
            return [false, '账户余额不足'];
        }

        $apply['user_id']   =   $user_info['user_id'];
        $apply['amount']    =   $withdraw_money;
        $apply['withdraw_no']=  createRandomNumber(24);
        $apply['remark']    =   '用户申请提款';
        $apply['status']    =   ApplyWithdraw::APPLY_INIT;

        $apply_result   =   ApplyWithdraw::create($apply);
        if ($apply_result) {
            UserModel::where(['uid' => $user_info['user_id']])->update(['balance' => $user_info['balance'] - $withdraw_money]);
            return [true, ['withdraw_id' => $apply_result->id]];
        }
        return [false, '申请失败'];
    }

    /**
     * 申请提现进度查看
     * @param int $withdraw_id
     * @return array
     */
    public function commissionProcess(int $withdraw_id)
    {
        $user_info  =   request()->post('api_user');
        $apply_info =   ApplyWithdraw::where(['id' => $withdraw_id, 'user_id' => $user_info['user_id']])->select(['id', 'user_id', 'amount', 'status', 'withdraw_no', 'created_at', 'operate_time', 'pay_time'])->first();
        if (empty($apply_info)) {
            return [false, '提现记录不存在'];
        }
        $apply_info->apply_desc =   ApplyWithdraw::APPLY_DESC[$apply_info->status];
        $apply_info->apply_time =   $apply_info->created_at->toDateTimeString();
        $apply_info->deal_time  =   $apply_info->operate_time == 0 ? '' : $apply_info->operate_time->toDateTimeString();
        $apply_info->pay_time   =   $apply_info->pay_time == 0 ? '' : $apply_info->pay_time->toDateTimeString();
        unset($apply_info->created_at);
        return [true, $apply_info];
    }

    /**
     * 用户提现记录
     * @return array
     */
    public function commissionRecord()
    {
        $user_info  =   request()->post('api_user');
        $apply_list =   ApplyWithdraw::where(['user_id' => $user_info['user_id']])->select(['id', 'user_id', 'amount', 'status', 'withdraw_no', 'pay_time', 'pay_amount', 'remark', 'created_at'])->get()->each(function (&$item) {
                            $item->apply_time   =   $item->created_at->toDateTimeString();
                            $item->pay_time     =   Carbon::createFromTimestamp($item->pay_time)->toDateTimeString();
                            unset($item->created_at);
                        });
        return [true, $apply_list->isEmpty() ? [] : $apply_list->toArray()];
    }

}
